package edge.display.backend.Entity;

import edge.display.backend.Entity.ECharts.DynamicData;
import edge.display.backend.KEY_STORE;
import edge.display.backend.Utils.StatUtils;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 网络统计实体类
 *
 * @author Amane Hayaashi
 * @date 2020/11/8
 * @since 1.0
 */

@Data
public class NetStat {

    private ComNet comNet;

    private DynamicData netComputing;

    private DynamicData netStorage;

    private Queue<Object> xAxisDynamic;

    public NetStat() {
        netComputing = new DynamicData();
        netStorage = new DynamicData();
        xAxisDynamic = new LinkedList<>();
    }

    public void init() {
        netComputing.setYValue(new HashMap<String, Queue<Object>>() {
            {
                put(KEY_STORE.JSON_VALUE, new LinkedList<>());
            }
        });
        netStorage.setYValue(new HashMap<String, Queue<Object>>() {
            {
                put(KEY_STORE.JSON_VALUE, new LinkedList<>());
            }
        });
    }

    public void update(ComNet comNet) {
        this.comNet = comNet;
        updateNetDynamic();
    }

    private void updateNetDynamic() {
        String xVal = new SimpleDateFormat(KEY_STORE.NET_TIME_FORMAT).format(new Date(Long.parseLong(comNet.getId())));
        StatUtils.pushData(xAxisDynamic, xVal);
        // update y of netComputing
        Map<String, Queue<Object>> yValueComp = netComputing.getYValue();
        StatUtils.pushData(yValueComp, KEY_STORE.JSON_VALUE, comNet.getNetwork().getComputing());
        netComputing.setYValue(yValueComp);
        // update y of taskEff
        Map<String, Queue<Object>> yValueStorage = netStorage.getYValue();
        StatUtils.pushData(yValueStorage, KEY_STORE.JSON_VALUE, comNet.getNetwork().getStorage());
        netStorage.setYValue(yValueStorage);
    }
}
